﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AnimalCtrl : MonoBehaviour
{
    public float health;

    public string amType;
    public float range;
    public float maxSpeed;
    public float maxTorque;
    public Rigidbody rd;
    public NeuralNetwork nn;
    public Genome genome;
    public double fit;
    public double[] inputs;
    public double[] weights;
    public int[] splitPoints;
    public bool isActiveSpeed;
    public Slider slider;
    public Image jiasu;
    public bool isCanJiasu;

    public float jiasuCd;
    public float jiasuShijian;
    private float t1;
    private float t2;
    void Start()
    {
        rd = GetComponent<Rigidbody>();

        //for (int i = eyeCount - 1; i >= 0; i--)
        //{
        //    GameObject go = new GameObject(eyes.Count.ToString());
        //    go.transform.SetParent(transform);
        //    go.transform.localPosition = Vector3.zero;
        //    go.transform.localRotation = Quaternion.Euler(new Vector3(0, -i * angle));
        //    eyes.Add(go.transform);
        //}

        //for (int i = 1; i < eyeCount; i++)
        //{
        //    GameObject go2 = new GameObject(eyes.Count.ToString());
        //    go2.transform.SetParent(transform);
        //    go2.transform.localPosition = Vector3.zero;
        //    go2.transform.localRotation = Quaternion.Euler(new Vector3(0, i * angle));
        //    eyes.Add(go2.transform);
        //}

        ReSetPosition(transform);
    }

    void Update()
    {
        Jiasu();
        jiasu.gameObject.SetActive(isActiveSpeed);
        Run();
        CheckDie();
        fit = genome.fitness;
        weights = genome.weights;
        splitPoints = nn.splitPoints;
        slider.value = health / 50f;
        slider.transform.parent.rotation = Camera.main.transform.rotation;
    }

    public void Jiasu()
    {
        if (isActiveSpeed)
        {
            t2 += Time.deltaTime;
            if (t2 > jiasuShijian)
            {
                t2 = 0;
                isActiveSpeed = false;
                isCanJiasu = false;
            }
        }
        else if (!isCanJiasu)
        {
            t1 += Time.deltaTime;
            if (t1 > jiasuCd)
            {
                t1 = 0;
                isCanJiasu = true;
            }
        }
    }

    public void ReSetPosition(Transform startPosition)
    {
        transform.position = startPosition.position + new Vector3(Random.Range(-range / 2f, range / 2f), 0, Random.Range(-range / 2f, range / 2f));
        transform.rotation = startPosition.rotation;
    }

    public virtual void CheckDie()
    {
    }

    public virtual void Run()
    {

    }

    private void OnCollisionEnter(Collision other)
    {
        if (amType == "A" && other.gameObject.tag == "Food")
        {
            genome.fitness += 8;
            other.transform.position = new Vector3(Random.Range(-30, 30), 0, Random.Range(-30, 30));
        }
        if (amType == "B" && other.gameObject.tag == "A")
        {
            genome.fitness += 10;
            other.gameObject.GetComponent<AnimalCtrl>().health = 0;
        }
    }
}
